home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Amiga Format CD 6
/
Amiga Format AFCD06 (Nov 1996, Issue 90).iso
/
serious
/
shareware
/
text
/
wordwrap
/
wordwrap.c
< prev
next >
Wrap
C/C++ Source or Header
|
1996-08-18
|
5KB
|
159 lines
; /*
gcc -noixemul wordwrap.c -o wordwrap
quit 0 ; */
/*************************************************************************\
wordwrap.c:
Reformat a text on the input stream to a given maximum line length.
arguments and their meanings:
-l<len> line length, defaults to 75
-b protect blank lines
-i protect indentation
-i<indent> " , enforcing a fixed indentation width
-s<len> protect short lines
-m<width> add a left margin
-M<width> ignore a left margin on the input
\*************************************************************************/
#include <stdio.h>
#include <string.h>
char version[] = "$VER: wordwrap 1.1 (18.08.96)";
#define MAXLEN 200 /* max. size of an input word */
int shortchars = 0;
int getword(char s[], int lim);
void help(char *s);
int main(int argc, char* argv[])
{
int i, lword, lline, dented;
char *s, c, word[MAXLEN], newline[MAXLEN];
/* adjustable parameters: */
int lmax = 75, blanks = 0, indent = 0;
unsigned int imargin = 0, omargin = 0;
while (--argc) {
s = *++argv;
if (*s++ == '-') {
switch (*s++) {
case 'l': lmax = atoi(s); break;
case 'b': blanks = 1; break;
case 's': shortchars = atoi(s); break;
case 'm': omargin = atoi(s); break;
case 'M': imargin = atoi(s); break;
case 'i': indent = -1;
if (isdigit(*s)) indent = atoi(s);
break;
default: help(argv[0]); return 10;
}
} else {
help(argv[0]); return 10;
}
}
/* prepare the left margin string: */
if (omargin>MAXLEN-2) omargin = MAXLEN-2;
newline[0] = '\n';
for (i=1; i<=omargin; i++) newline[i] = ' ';
newline[omargin+1] = '\0';
/* let's go */
lline = 0; dented = 0;
while (lword = getword(word, MAXLEN)) {
if (word[0] == '\n') { /* has read a blank line */
if (blanks) {
if (lline) printf("%s", newline);
printf("%s", newline); lline = 0;
}
} else if (isspace(word[0])) { /* indented line */
if (indent && lword>imargin) {
if (lline) printf("%s", newline);
if (indent>0) {
for (lline = 0; lline<indent; lline++) putchar(' ');
} else {
printf("%s", &word[imargin]); lline = lword-imargin;
}
dented = 1;
}
} else { /* regular word */
if ((c = word[lword-1]) == '\n') /* "short line" break request? */
word[--lword] = '\0';
if (lline == 0 || dented) { /* at the start of the line */
printf("%s", word); lline += lword; dented = 0;
} else { /* append to an existing line */
if (lline + lword < lmax) {
printf(" %s", word); lline += lword + 1;
} else {
printf("%s%s", newline, word); lline = lword;
}
}
if (c == '\n') { /* "short line" break pending */
printf("%s", newline); lline = 0;
}
}
}
if (lline)
putchar('\n');
return 0;
}
void help(char *s)
/* print a help text and nag about illegal parameter <s> */
{
if (s) fprintf(stderr,"illegal option '%s'\n", s);
fprintf(stderr,"'wordwrap' command line parameters:\n", s);
fprintf(stderr," -l<len> line length, defaults to 75\n");
fprintf(stderr," -b protect blank lines\n");
fprintf(stderr," -i / -i<width> protect indentation\n");
fprintf(stderr," -s<len> protect lines shorter than <len>\n");
fprintf(stderr," -m<width> / -M<width> add/strip left margin\n");
}
int getword(char s[], int lim)
/* Copies one word of the input stream to s[] (return value is strlen(s)),
* then skips all subsequent spaces, stopping at the next word or EOL.
* - will return "\n" for an empty line
* - will return a string of blanks for a line starting with blanks
* - will append "\n" to the last word on a "short" line
* - will return an empty string at EOF
*/
{
int c, i = 0;
static int nonblanks = 0;
c = getchar();
if (c == EOF) {
s[0] = '\0'; return 0;
}
if (isspace(c)) { /* indented line (or even a blank line?) */
while (isspace(c) && c != '\n') {
if (i<lim-1) s[i++] = c;
c = getchar();
}
if (c == '\n' || c == EOF) {
i = 0; s[i++] = '\n'; /* yeah, a blank line */
}
} else { /* read a word of non-blanks */
while (isgraph(c)) {
if (i<lim-1)
s[i++] = c;
else {
fprintf(stderr, "warning: long input word (> %d chars)\n", lim);
break;
}
c = getchar();
}
nonblanks += i;
while (isspace(c) && c != '\n') /* skip blanks */
c = getchar();
if (c == '\n') { /* was this a "short line"? */
if (nonblanks <= shortchars) s[i++] = '\n';
nonblanks = 0;
}
}
if (isgraph(c)) /* did we stop on a non-blank character? */
ungetc(c, stdin);
s[i] = '\0'; return i;
}